package com.yanqu.road.server.cmd.activity.douluodalu;

import com.yanqu.road.entity.activity.douluodalu.config.DouLuoDaLuConfig;
import com.yanqu.road.entity.activity.douluodalu.enums.eDouLuoDaLuEventType;
import com.yanqu.road.entity.config.goods.OpenGoodsBagResult;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.log.LogDouLuoDaLuOpenBox;
import com.yanqu.road.pb.activity.douluodalu.DouLuoDaLuProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.douluodalu.DouLuoDaLuModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.douluodalu.DouLuoDaLuMgr;
import com.yanqu.road.server.manger.config.GoodsBagMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;


@Cmd(code = Protocol.S_DOU_LUO_DA_LU_OPEN_BOX_FROM_CROSS,desc = "打开宝箱")
public class DouLuoDaLuOpenBoxCmdFromCross extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        DouLuoDaLuProto.DouLuoDaLuOpenBoxToGameServerMsg reqMsg = DouLuoDaLuProto.DouLuoDaLuOpenBoxToGameServerMsg.parseFrom(packet.getBytes());
        ret = reqMsg.getRet();
        int num = reqMsg.getNum();
        DouLuoDaLuConfig config = DouLuoDaLuMgr.getConfig();

        String consume = config.getBoxOpenConsume().get(reqMsg.getBoxId());
        Property consumeProperty = PropertyHelper.parseStringToProperty(consume);
        if(num > 1){
            consumeProperty.rideProperty(num);
        }
        player.getModule(PreReduceModule.class).restorePreReduce(consumeProperty);

        DouLuoDaLuProto.DouLuoDaLuOpenBoxRespMsg.Builder builder = DouLuoDaLuProto.DouLuoDaLuOpenBoxRespMsg.newBuilder().setRet(ret);
        if(ret == 0){
            // 扣道具
            CurrencyModule module = player.getModule(CurrencyModule.class);
            module.removeCurrency(consumeProperty, eLogMoneyType.DouLuoDaLuActivity, eLogMoneyType.DouLuoDaLuMonsterBoxOpenConsume);
            // 获得道具
//            Property reward = new Property();
//            for (int i = 0; i < reqMsg.getNum(); i++) {
//                OpenGoodsBagResult openGoodsBagResult = GoodsBagMgr.getAllGoods(reqMsg.getBoxId());
//                reward.addProperty(openGoodsBagResult.getReward());
//            }
            module.addCurrency(reqMsg.getReward(), eLogMoneyType.DouLuoDaLuActivity, eLogMoneyType.DouLuoDaLuMonsterBoxEventReward);
//            String rewardStr = PropertyHelper.parsePropertyToString(reward);

            builder.setReward(reqMsg.getReward());
            builder.setBoxId(reqMsg.getBoxId());

            //开宝箱事件
            if(config.getBoxShowLog().contains(reqMsg.getBoxId())) {
                player.getModule(DouLuoDaLuModule.class).uploadEventNotice(eDouLuoDaLuEventType.MonsterBoxEvent.getIntValue(), 0, reqMsg.getReward(), consume);
            }

            //日志
            AutoLogMgr.add(new LogDouLuoDaLuOpenBox(DouLuoDaLuMgr.getActivityInfo().getActivityId(), player.getUserId(), reqMsg.getTeamId(), reqMsg.getRegionId(), reqMsg.getBoxId(), num, reqMsg.getReward()));
        }
        builder.setRet(ret);
        player.sendPacket(Protocol.U_DOU_LUO_DA_LU_OPEN_BOX, builder);
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
